#include "ImagePreprocessingSettings.h"
#include "ui_ImagePreprocessingSettings.h"
#include <QRegExpValidator>

ImagePreprocessingSettings::ImagePreprocessingSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ImagePreprocessingSettings)
{
    // 设置无边框窗口
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setMouseTracking(true);
    ui->setupUi(this);
    
    // 连接信号和槽
    connect(ui->pushButtonOK, &QPushButton::clicked, this, &ImagePreprocessingSettings::onPushButtonOKClicked);
    connect(ui->pushButtonCancel, &QPushButton::clicked, this, &ImagePreprocessingSettings::onPushButtonCancelClicked);
    
    // 创建正则表达式验证器，限制输入为整数坐标格式："x,y"，其中x和y都是整数
    QRegExp regExp("^\\d+,\\d+$");
    QRegExpValidator *validator = new QRegExpValidator(regExp, this);
    
    // 为所有坐标输入框设置正则表达式验证器
    ui->lineEditTopLeft->setValidator(validator);
    ui->lineEditTopRight->setValidator(validator);
    ui->lineEditBottomLeft->setValidator(validator);
    ui->lineEditBottomRight->setValidator(validator);

}

ImagePreprocessingSettings::~ImagePreprocessingSettings()
{
    delete ui;
}

void ImagePreprocessingSettings::mousePressEvent(QMouseEvent *event)
{
    // 检查鼠标事件是否为左键按下，并且鼠标位置是否在标题栏区域内
    if (event->button() == Qt::LeftButton &&
        ui->titleWidget->geometry().contains(event->pos())) {
        // 标记窗口正在被拖动
        isDragging = true;
        // 计算鼠标位置与窗口左上角的偏移量
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        // 接受该事件，表示事件已被处理
        event->accept();
    } else {
        // 如果不是左键点击标题栏，则调用基类的鼠标按下事件处理函数
        QDialog::mousePressEvent(event);
    }
}

void ImagePreprocessingSettings::mouseMoveEvent(QMouseEvent *event)
{
    // 检查是否处于拖动状态 && 左键是否保持按下
    if (isDragging && (event->buttons() & Qt::LeftButton)) {
        // 计算并移动窗口到新位置
        move(event->globalPos() - dragPosition);
        // 标记事件已处理，阻止继续传播
        event->accept();
    } else {
        // 非拖动状态，调用基类默认处理
        QDialog::mouseMoveEvent(event);
    }
}

void ImagePreprocessingSettings::mouseReleaseEvent(QMouseEvent *event)
{
    isDragging = false;  // 重置拖动状态标志，标记拖动操作结束
    event->accept();     // 接受该事件，表明事件已被处理
}

void ImagePreprocessingSettings::setParameters(const QVariantMap &params)
{
    // 设置裁剪参数
    if (params.contains(PARAM_ENABLE_CROP)) {
        ui->checkBoxEnableCrop->setChecked(params[PARAM_ENABLE_CROP].toBool());
    }
    
    if (params.contains(PARAM_TOP_LEFT)) {
        ui->lineEditTopLeft->setText(params[PARAM_TOP_LEFT].toString());
    }
    
    if (params.contains(PARAM_TOP_RIGHT)) {
        ui->lineEditTopRight->setText(params[PARAM_TOP_RIGHT].toString());
    }
    
    if (params.contains(PARAM_BOTTOM_LEFT)) {
        ui->lineEditBottomLeft->setText(params[PARAM_BOTTOM_LEFT].toString());
    }
    
    if (params.contains(PARAM_BOTTOM_RIGHT)) {
        ui->lineEditBottomRight->setText(params[PARAM_BOTTOM_RIGHT].toString());
    }
    
    // 设置旋转参数
    if (params.contains(PARAM_ROTATION_ANGLE)) {
        ui->spinBoxRotationAngle->setValue(params[PARAM_ROTATION_ANGLE].toInt());
    }
    
    // 设置镜像参数
    if (params.contains(PARAM_MIRROR_TYPE)) {
        int mirrorType = params[PARAM_MIRROR_TYPE].toInt();
        if (mirrorType >= 0 && mirrorType < ui->comboBoxMirrorType->count()) {
            ui->comboBoxMirrorType->setCurrentIndex(mirrorType);
        }
    }
}

QVariantMap ImagePreprocessingSettings::collectParameters() const
{
    QVariantMap params;
    
    // 收集裁剪参数

    params[PARAM_ENABLE_CROP] = ui->checkBoxEnableCrop->isChecked();
    params[PARAM_TOP_LEFT] = ui->lineEditTopLeft->text();
    params[PARAM_TOP_RIGHT] = ui->lineEditTopRight->text();
    params[PARAM_BOTTOM_LEFT] = ui->lineEditBottomLeft->text();
    params[PARAM_BOTTOM_RIGHT] = ui->lineEditBottomRight->text();
    
    // 收集旋转参数
    params[PARAM_ROTATION_ANGLE] = ui->spinBoxRotationAngle->value();
    
    // 收集镜像参数
    params[PARAM_MIRROR_TYPE] = ui->comboBoxMirrorType->currentIndex();

    return params;
}

void ImagePreprocessingSettings::onPushButtonOKClicked()
{
    accept();
}

void ImagePreprocessingSettings::onPushButtonCancelClicked()
{
    reject();
}

void ImagePreprocessingSettings::on_checkBoxEnableCrop_toggled(bool checked)
{
    ui->widgetCropPosition->setEnabled(checked);
    ui->labelTopLeft->setEnabled(checked);
    ui->labelTopRight->setEnabled(checked);
    ui->labelBottomLeft->setEnabled(checked);
    ui->labelBottomRight->setEnabled(checked);
}
